Skip to content

feat(textstream): 词典热词进入 TextStream 本地识别 - #1498

Open
chenyue42 wants to merge 1 commit into
Open-Less:betafrom
chenyue42:feat/textstream-dictionary-hotwords
Open

chenyue42 wants to merge 1 commit into
Open-Less:betafrom
chenyue42:feat/textstream-dictionary-hotwords

Conversation

@chenyue42

Copy link
Copy Markdown

摘要

无对应 issue。之前和维护者沟通过,TextStream 目前拿不到词典,词典只在 LLM 润色时生效。

这个 PR 让已启用的词典词条作为 Qwen3-ASR 的 context 参与 TextStream 识别,在语音识别这一步就把专有名词写对,而不是全靠后面的润色去补。

修复 / 新增 / 改进

  • 新增:热词进入识别。 沿用会话开始时冻结的 asr.prompt(同一排序与字符预算),经环境变量 OPENLESS_TEXTSTREAM_CONTEXT 交给 worker,再传给 init_streaming(context=...)。选区流程清空 asr.prompt 时不注入;内容为空或不可用时按无词典识别,不影响录音启动。
  • 格式转换。 asr.prompt 是 A, B.,而 mlx-qwen3-asr 文档要求空格分隔。实测逗号列表会诱使解码器在正常语句中接着“续写”词表并丢掉后半句,空格分隔没有出现这种情况,所以 worker 侧转成空格分隔。同时去掉 < >:词条里出现 <|audio_pad|> 之类的写法会被分词成控制 token,导致每次录音失败。
  • 背诵检测与重启。 带 context 后,没有人声的窗口会让解码器把整段 context 当作转写输出,且残留在强制前缀里吞掉之后的语音。worker 检测“连续 4 个及以上词条、顺序与词典一致”的片段(按词条计数,多词短语算一个;只匹配完整的词;词条间的标点和空格可以不同),从所有快照中去掉并重启流。触发的那一块音频会在新流里单独再解码一次,以免丢掉恰好在其中开始的语音;仍是背诵才丢弃,并像录音开始时一样等待人声。思路与 textstream-asr 原项目 HALLUCINATION_PATTERNS 的“检测后重置”一致,只是匹配对象由固定句子换成当前词典。
  • 语言误判重置。 带 context 时,人声很少的 chunk 可能被标成 None 并强制沿用到之后的窗口,使中文窗口之间多出空格;检测到后重置为未判定。
  • 升级兼容。 worker.py 变了,把 v2.0.2-Beta.4 至 v2.0.3-Beta.3 的 worker 加入 PREVIOUS_WORKERS,否则已安装用户升级后会被判为未安装。另外顺手补了 v2.0.2-Beta.1 至 Beta.3 那一版(fix(textstream): 快照前缀被改写时保留权威全文,不再中止整条转写 #1257 改 worker 时漏加),与本功能无关,维护者不需要可以去掉。

兼容

  • 不包含:UI 开关(与其他支持热词的渠道一致,默认生效);文档改动(docs/development/local-models.md 已在上游移除);aoci.code.txt 索引(本地没有 aoci 工具)。
  • 对现有用户 / 本地环境 / 构建流程的影响:
    • 不带词典时,新增分支都不会进入。11 个场景下与当前 beta 的 worker 输出逐字相同。
    • 带词典的用户,TextStream 的识别结果会变化,这是本 PR 的目的。
    • 已安装 TextStream 的用户首次录音时会自动替换 .runtime/worker.py,不需要重装运行组件。
  • 已知局限:
    • 背诵检测是启发式的。词典不足 4 个词条时不检测,因为此时无法区分“背诵”和“用户真的说了这几个词”;静音窗口可能输出那几个词。
    • 语言误判的重置只在 feed_audio 返回后进行;30 秒窗口提交发生在同一次调用内时仍可能把 None 带入后续窗口。修复需要改库的非公开状态,未做。
    • context 是通过系统提示实现的,背诵只能压低不能根除。更彻底的做法是解码时做词表偏置,或由 mlx-qwen3-asr 自身避免解码无人声的窗口,都超出本 PR 范围。
    • 英文切换到中文时偶发误识别,在当前 beta 上同样存在,本 PR 未处理。

测试计划

  • 命令:python3 -B -m unittest discover -s src-tauri/src/asr/local/experimental/textstream -p 'test_*.py'

  • 结果:20 个通过(原有 13 个)。新增用例逐个做了反向验证:撤掉对应实现后用例会失败。注意 CI 目前不运行这个文件。

  • 命令:cargo test --locked --manifest-path src-tauri/Cargo.toml --lib textstream -- --test-threads=1

  • 结果:12 通过,2 个原本就标记 ignored。

  • 命令:npm test

  • 结果:通过。local-asr-lifecycle-contract.test.mjs 新增了接线断言,以及 Rust 与 Python 两侧环境变量名一致的检查。

  • 真实模型回归(本机已安装的 Qwen3-ASR 0.6B,经 worker 的真实 stdin/stdout 协议):

    • 36 个静音与停顿场景,分别用 7 词和 21 词两套词典:全部正常退出,最终文本和所有预览中均无背诵,句子数全部正确;不加检测时同样的场景约三分之一出现背诵。
    • 11 段连续语音(两种英文口音、中英混说、无热词对照),21 词词典:无背诵、无内容缺失;写对的专有名词由 13 个增至 36 个。
  • 真实麦克风录音一段(约 100 秒,中英混说,含两次 10 秒停顿和结尾静音),同一段音频对比:

    版本 结果
    当前 beta,无词典 onion pure、RW、C D K S、Openness;结尾多出 "The."
    本 PR,无词典 与上一行逐字相同
    本 PR,带词典 OnionPIR、RLWE、CDKS、OpenLess、server-stateful;无背诵
    仅空格格式、不加检测 专有名词同上,但最终文本中出现 2 次整段词典,预览中出现 21 次
  • 未验证:合成语音之外只有一位说话人的录音;没有覆盖耳语、多人、嘈杂环境;没有做系统性的词错率评测。背诵匹配规则与“再解码一次”是最后一轮修改,只经过上述测试。

  • 证据路径:回归脚本与各场景输出可按需提供。

测试过程中发现的两个与本 PR 无关的问题,另行反馈:debug 构建在启动时于 permissions.rs 的 authorizationStatusForMediaType: 调用处 panic(release 构建不受影响);127.0.0.1:7890 被其他程序占用时 TextStream 无法启动。

🤖 Generated with Claude Code

已启用的词典词条作为 Qwen3-ASR 的 context 参与 TextStream 识别,
在语音识别这一步就把专有名词写对,而不是只靠 LLM 润色去补。

- 沿用会话冻结的 asr.prompt,经环境变量 OPENLESS_TEXTSTREAM_CONTEXT
  交给 worker,再传给 init_streaming(context=...)。
- worker 把 "A, B." 转成 mlx-qwen3-asr 文档要求的空格分隔:逗号列表会
  诱使解码器在正常语句中续写词表。去掉尖括号,避免词条被分词成控制 token。
- 没有人声的窗口会让解码器把 context 当作转写输出,并残留在强制前缀里
  吞掉之后的语音。检测连续 4 个及以上、顺序与词典一致的词条,从所有快照
  中去掉并重启流;触发的那块音频在新流里再解码一次。
- 带 context 时被标成 None 的语言不再沿用到后续窗口。
- 把已发布的 worker 加入 PREVIOUS_WORKERS,已安装用户可直接升级脚本。

不带词典时行为与之前一致。

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T02:48:02.207478Z 18caeb0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 18caeb0348

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +389 to +390
done = spoken(state.text)
state = decode(audio, new_stream())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reconcile the triggering block before appending its replay

With at least four hotwords, a block can contain both real speech and a recitation. For example, RecitingSession returning Hello. {recited} on the first decode and Hello. on replay produces final text Hello. Hello.: this assignment has already retained the current block's speech in done, and spoken() then appends its replay a second time. Preserve the pre-block prefix or reconcile overlapping text before accumulating the replay.

Useful? React with 👍 / 👎.

Comment on lines +391 to +393
if recited.search(state.text):
state = new_stream()
heard_speech = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain speech from a retry that also contains recitation

With at least four hotwords, if the poisoned stream returns only {recited} but replay recovers Hello. {recited}, this branch discards Hello. along with the recitation. The existing fake-session harness reproduces empty streamed and final output for that case, because done is never updated from the retry and heard_speech = False bypasses finalization. Retain the retry's cleaned speech before replacing its state, with overlap reconciliation.

Useful? React with 👍 / 👎.

Comment on lines +59 to +61
r"[\s,,、]*".join(terms[start:end])
for end in range(len(terms), RECITED_PHRASES - 1, -1)
for start in range(end - RECITED_PHRASES + 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bound the recitation matcher's startup complexity

With many short dictionary entries, enumerating every contiguous subsequence produces a cubic-sized regex even though build_asr_prompt caps input at 240 characters. A Python 3.12.3 reproduction with 60 two-character entries (239 prompt characters total) builds a 363,151-character pattern, takes about 9.5 seconds to compile, and peaks at about 170 MiB RSS. This runs before ready on every recording, substantially delaying startup for valid dictionaries. Bound matcher complexity rather than expanding all subsequence lengths.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants